/*
 * Hidden Equity LLC
 * Author: Maksym Fedyay, Kyiv, mcsimf@gmail.com
 * Date: 29.02.2012
 */

package com.hiddenequity.adgroup_service;

import java.util.ArrayList;

import com.hiddenequity.acts.Act_Super;
import com.hiddenequity.utils.Converter;
import com.hiddenequity.utils.SOAPUtils;
import com.hiddenequity.utils.UrlBuncher;

public class AdGroupService {

	public static final String URL_ADGROUP_SERVICE = UrlBuncher.URL_BASE + "AdGroupService";

	/*
	 * Get AdGroup List
	 */
	static public String getAdGroupList(String campaignId, ArrayList<String> dateRange, int status){
		
		String SOAPBody =
				"<soap:Body>" +
					"<get>" +
						"<serviceSelector>" +
							"<fields>Id</fields>" +
							"<fields>Name</fields>" +
							"<fields>CampaignId</fields>" +
							"<fields>Status</fields>" +
							"<fields>Clicks</fields>" +
							"<fields>Ctr</fields>" +
							"<fields>Cost</fields>" +
							"<fields>Impressions</fields>" +
							"<fields>AverageCpc</fields>";
							
		if(status != 0){
			String statusName = "";
			switch (status) {
				case Act_Super.STATUS_ACTIVE:
					statusName = "ENABLED";
					break;
				case Act_Super.STATUS_PAUSED:
					statusName = "PAUSED";
					break;
				case Act_Super.STATUS_DELETED:
					statusName = "DELETED";
					break;
			}
			
			SOAPBody = SOAPBody + 
				"<predicates>" +
					"<field>Status</field>" +
					"<operator>EQUALS</operator>" +
					"<values>" + statusName + "</values>" +
				"</predicates>";
		}							
							
		SOAPBody = SOAPBody + "<predicates>" +
								"<field>CampaignId</field>" +
								"<operator>EQUALS</operator>" +
								"<values>" + campaignId + "</values>" +
							"</predicates>";
		if(!dateRange.get(0).equals("")){
			SOAPBody = SOAPBody +	"<dateRange>" +
					"<min>" + dateRange.get(0) +"</min>" +
					"<max>" + dateRange.get(1) +"</max>" +
					"</dateRange>";
		}
							
							
		SOAPBody = SOAPBody + 
							"<ordering>" +
								"<field>Clicks</field>" +
								"<sortOrder>DESCENDING</sortOrder>" +
							"</ordering>" +
						"</serviceSelector>" +
					"</get>" +
				"</soap:Body>";
		
		String envelope = SOAPUtils.buildEnvelope(SOAPBody);
		return envelope;
	}//getAdGroupList
	

	
	/*
	 * Get AdGroup By Id
	 */
	static public String getAdGroupById(String adGroupId){
		
		String SOAPBody =
				"<soap:Body>" +
					"<get xmlns=" + SOAPUtils.XMLNS +
						"xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
						"<serviceSelector>" +
							"<fields>Id</fields>" +
							"<fields>Name</fields>" +
							"<fields>Status</fields>" +
							"<fields>KeywordMaxCpc</fields>" +
							"<fields>KeywordContentMaxCpc</fields>" +
							"<fields>ProxyKeywordMaxCpc</fields>" +
							"<predicates>" +
								"<field>Id</field>" +
								"<operator>EQUALS</operator>" +
								"<values>" + adGroupId + "</values>" +
							"</predicates>" +
						"</serviceSelector>" +
					"</get>" +
				"</soap:Body>";
		
		String envelope = SOAPUtils.buildEnvelope(SOAPBody);
		return envelope;		
	}
	
	
	/*
	 * Add AdGroup
	 */
	static public String adGroupOperation(String name,  String campaignId, String adGoupId,
			String campaignBidingStrategy, String defaultBids, String networkBids){
		
		defaultBids = Converter.strToMicroamount(defaultBids);
		networkBids = Converter.strToMicroamount(networkBids);
		
		//Operation selector
		String operator = "";
		String campaignIdTag = "";
		String adGoupIdTag = "";
		if(campaignId == null){
			operator = "SET";
			adGoupIdTag = "<id>" + adGoupId + "</id>";
		} else {
			operator = "ADD";
			campaignIdTag = "<campaignId>" + campaignId + "</campaignId>";	
		}
		
		String SOAPBody =
				"<soap:Body>" +
					"<mutate xmlns=" + SOAPUtils.XMLNS +
						"xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
						"<operations>" +
							"<operator>" + operator + "</operator>" +
							"<operand>" +
								campaignIdTag +
								adGoupIdTag +
								"<name>" + name + "</name>";
		
		if(campaignBidingStrategy.equals("ManualCPC")){
			SOAPBody = SOAPBody + 
				"<bids xsi:type='ManualCPCAdGroupBids'>" +
					"<keywordMaxCpc>" +
						"<amount><microAmount>" + defaultBids +"</microAmount></amount>" +
					"</keywordMaxCpc>" +
					"<keywordContentMaxCpc>" +
						"<amount><microAmount>" + networkBids + "</microAmount></amount>" +
					"</keywordContentMaxCpc>" +
				"</bids>";
		} else if(campaignBidingStrategy.equals("BudgetOptimizer")){
			SOAPBody = SOAPBody + 
				"<bids xsi:type='BudgetOptimizerAdGroupBids'></bids>";			
		}

		SOAPBody = SOAPBody + 
							"</operand>" +
						"</operations>" +
					"</mutate>" +
				"</soap:Body>";
		
		String envelope = SOAPUtils.buildEnvelope(SOAPBody);
		return envelope;
	}//addAdGroup
	


	/*
	 * Set adgroup status
	 */
	static public String setAdGroupStatus(String campaignId, String adGroupId, int status){
		String statusName = "";
		
		if(status != 0){
			switch (status) {
				case Act_Super.STATUS_ACTIVE:
					statusName = "ENABLED";
					break;
				case Act_Super.STATUS_PAUSED:
					statusName = "PAUSED";
					break;
				case Act_Super.STATUS_DELETED:
					statusName = "DELETED";
					break;
			}
		}
		
		String SOAPBody = 
			"<soap:Body>" +
				"<mutate xmlns=" + SOAPUtils.XMLNS +
					"xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
					"<operations>" +
						"<operator>SET</operator>" +
						"<operand>" +
							"<id>" + adGroupId + "</id>" +
							"<campaignId>" + campaignId + "</campaignId>" +
							"<status>"+ statusName + "</status>" +
						"</operand>" +
					"</operations>" +
				"</mutate>" +
			"</soap:Body>";		
		String envelope = SOAPUtils.buildEnvelope(SOAPBody);
		return envelope;
	}//setAdGroupStatus
	
			
}
